package race;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.Timer;
import java.util.TimerTask;

public class GamePanel extends Canvas {

    /**
     * Game Loop Variables *
     */
    private Timer gameTimer;
    private TimerTask gameLoopTask;
    private int fps;
    private long frameTime, lastTime;
    
    /**
     * Game Variables *
     */
    private Car player1;
    private Car player2;

    public GamePanel() {
        setSize(800, 600);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                switch (keyCode) {
                    case KeyEvent.VK_ESCAPE: {
                        System.exit(0);
                        break;
                    }
                    case KeyEvent.VK_W: {
                        player1.setUp(true);
                        break;
                    }
                    case KeyEvent.VK_A: {
                        player1.setLeft(true);
                        break;
                    }
                    case KeyEvent.VK_D: {
                        player1.setRight(true);
                        break;
                    }
                    case KeyEvent.VK_S: {
                        player1.setDown(true);
                        break;
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                int keyCode = e.getKeyCode();
                switch (keyCode) {
                    case KeyEvent.VK_W: {
                        player1.setUp(false);
                        break;
                    }
                    case KeyEvent.VK_A: {
                        player1.setLeft(false);
                        break;
                    }
                    case KeyEvent.VK_D: {
                        player1.setRight(false);
                        break;
                    }
                    case KeyEvent.VK_S: {
                        player1.setDown(false);
                        break;
                    }
                }
            }
        });
    }

    public void startGame() {
        createBufferStrategy(2);
        requestFocus();

        player1 = new Car(this);
        
        gameTimer = new Timer();
        
        frameTime = 0;
        
        if(gameLoopTask != null){
            gameLoopTask.cancel();
        }
        lastTime = System.nanoTime();
        gameLoopTask = new TimerTask(){
            @Override
            public void run(){
                long now = System.nanoTime();
                frameTime += (now - lastTime);
                lastTime = now;
                if(frameTime >= 1000000000){
                    System.out.println("FPS: " + fps);
                    fps = 0;
                    frameTime = 0;
                }
                doGameUpdates();
                render();
                fps++;
            }
        };
        
        gameTimer.schedule(gameLoopTask, 0, 16);
    }

    public void stopGame() {
        gameLoopTask.cancel();
    }

    private void doGameUpdates() {
        player1.update();
    }

    private void render() {
        BufferStrategy bf = this.getBufferStrategy();
        Graphics g = null;
        try {
            g = bf.getDrawGraphics();
            g.clearRect(0, 0, getWidth(), getHeight());
            player1.render(g);
        } finally {
            g.dispose();
        }
        bf.show();
        Toolkit.getDefaultToolkit().sync();
    }
}
